//
//  FileManager.m
//  xiaoTest
//
//  Created by xiao_shoubao on 16/9/8.
//  Copyright © 2016年 xiao_shoubao. All rights reserved.
//

#import "FileManager.h"

@implementation FileManager

#pragma +++++++++++++++++++++++图片操作+++++++++++++++++++++++++
//以png形式保存图片到
+ (void)helpToolImagePNGWithName:(NSString *)imageName Image:(UIImage *)image
{
    NSData* imageData = UIImagePNGRepresentation(image);
    NSString* ceches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString* fullPathToFile = [ceches stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPathToFile atomically:NO];
}
//以jpg形式保存图片到

+ (void)helpToolImageJPEGWithName:(NSString *)imageName Image:(UIImage *)image CompressionQuality:(CGFloat )compressionQuality{
    
    NSData *imageData =UIImageJPEGRepresentation(image, compressionQuality);
    NSString* ceches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString* fullPath = [ceches stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPath atomically:NO];
    
}

+ (void)helpToolImageJPEGWithName:(NSString *)imageName Image:(UIImage *)image{
    
    NSData *imageData =UIImageJPEGRepresentation(image, 0.3);
    NSString* ceches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString* fullPath = [ceches stringByAppendingPathComponent:imageName];
    
    
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

#pragma +++++++++++++++++++++++++++++++++++++++++++++++

//获取路径
+ (NSString *)helpToolDataPathFile:(NSString *)name{

    NSString* ceches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString* plistPath = [ceches stringByAppendingPathComponent:name];
    
    return plistPath;
}

//建立文件夹
+ (void)helpToolCreateFolderAtPath:(NSString *)name{
    
    NSString* ceches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString* path = [ceches stringByAppendingPathComponent:name];
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:path]) {
        BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        
        NSLog(@"创建目录%zi",bo);
    }
}
+ (void)helpToolCreateFileAtPath:(NSString *)name{
    
    //建立文件管理
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString* ceches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString* plistPath = [ceches stringByAppendingPathComponent:name];
    if (![fm fileExistsAtPath:plistPath]) {
        
        [fm createFileAtPath:plistPath contents:nil attributes:nil];
    }
}

//复制plist文件
+ (void)helpToolBundleCopyTodocument:(NSString *)path Name:(NSString *)name{
    
    [self helpToolBundleCopyTodocument:path Name:name Type:@"plist"];
    
}

//复制文件
+ (void)helpToolBundleCopyTodocument:(NSString *)path Name:(NSString *)name Type:(NSString *)type{
    
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString* ceches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString* plistPath = [ceches stringByAppendingPathComponent:path];
    if (![fm fileExistsAtPath:path]) {
        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
        BOOL bo = [fm copyItemAtPath:bundlePath toPath:plistPath error:nil];
        
        NSLog(@"%@导入%i",name,bo);
    }
}


//覆盖文件
+ (void)helpToolBundleCoverTodocument:(NSString *)path Name:(NSString *)name Type:(NSString *)type{
    
    NSString* ceches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString* plistPath = [ceches stringByAppendingPathComponent:path];
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
    NSMutableDictionary *diclist = [[NSMutableDictionary alloc]initWithContentsOfFile:bundlePath];
    BOOL bo =[diclist writeToFile:plistPath atomically:YES];
    
    NSLog(@"%@覆盖%i", name,bo);
}

///读取资源文件
+ (NSString *)helpToolReadResouceFile:(NSString *)filename andType:(NSString *)type
{
    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:type];
    NSString *content = [NSString  stringWithContentsOfFile:path encoding: NSUTF8StringEncoding  error:NULL];
    return content;
}


@end
